import java.util.ArrayList;
import java.util.Arrays;

public class Permutation {
    private ArrayList<String> ret;
    private StringBuilder path;
    private boolean[] vis;
    public ArrayList<String> Permutation (String str) {
        // write code here
        ret = new ArrayList<>();
        path = new StringBuilder();
        char[] c = str.toCharArray();
        Arrays.sort(c);
        vis = new boolean[c.length];
        dfs(c);
        return ret;
    }

    private void dfs(char[] c) {
        if (path.length() == c.length) {
            ret.add(path.toString());
            return;
        }
        for (int i = 0; i < c.length; i++) {
            if (!vis[i] && (i == 0 || c[i] != c[i-1] || vis[i-1])) {
                vis[i] = true;
                path.append(c[i]);
                dfs(c);
                vis[i] = false;
                path.deleteCharAt(path.length()-1);
            }
        }
    }

    public static void main(String[] args) {
        Permutation p = new Permutation();
        System.out.println(p.Permutation("aab"));
    }
}
